package com.ruoyi.delivery.service.impl;

import com.ruoyi.delivery.domain.DelAllocationDetail;
import com.ruoyi.delivery.domain.DelAllocationOrder;
import com.ruoyi.delivery.mapper.DelAllocationDetailMapper;
import com.ruoyi.delivery.mapper.DelAllocationOrderMapper;
import com.ruoyi.delivery.service.IDelAllocationOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 调拨单 服务层实现
 *
 * @author hu
 */
@Service
public class DelAllocationOrderServiceImpl implements IDelAllocationOrderService {

    @Autowired
    private DelAllocationOrderMapper allocationOrderMapper;
    @Autowired
    private DelAllocationDetailMapper allocationDetailMapper;
    @Override
    public int countOperatorTransportOrders() {
        return allocationOrderMapper.countOperatorTransportOrders();
    }
    @Override
    public String generateTrackingNumber() {
        int count = countOperatorTransportOrders();
        String trackingNumber;
        trackingNumber = "T" + String.format("%012d", count + 1);
        return trackingNumber;
    }
    @Override
    public List<DelAllocationOrder> filterAllocationOrders(
            String allocationNo,
            String origin,
            String destination,
            String departStartDate,
            String departEndDate,
            String arriveStartDate,
            String arriveEndDate,
            String state,
            String orderNo,
            int page,
            int pageSize
    ) {
        // 计算分页的起始偏移量
        int offset = (page - 1) * pageSize;
        return allocationOrderMapper.filterAllocationOrders(
                allocationNo,
                origin,
                destination,
                departStartDate,
                departEndDate,
                arriveStartDate,
                arriveEndDate,
                state,
                orderNo,
                offset,
                pageSize
        );
    }

    @Override
    public boolean insertAllocationOrder(DelAllocationOrder order) {
        return allocationOrderMapper.insertAllocationOrder(order) > 0;
    }

    @Override
    public boolean deleteAllocationOrder(String allocationNo) {
        return allocationOrderMapper.deleteAllocationOrder(allocationNo) > 0;
    }

    @Override
    public boolean updateAllocationOrder(DelAllocationOrder order) {
        return allocationOrderMapper.updateAllocationOrder(order) > 0;
    }
    @Override
    public List<DelAllocationDetail> getDetailsByAllocationNo(String AllocationNo) {
        return allocationDetailMapper.getDetailsByAllocationNo(AllocationNo);
    }
}
